package labox.innovation.gameserver.handler;

import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javolution.util.FastMap;
import labox.innovation.config.Config;

/**
 * This class ...
 *
 * @version $Revision: 1.1.2.1.2.5 $ $Date: 2005/03/27 15:30:09 $
 */
public class UserCommandHandler {
	private static Logger _log = LoggerFactory.getLogger(UserCommandHandler.class.getName());

	private Map<Integer, IUserCommandHandler> _datatable;

	public static UserCommandHandler getInstance() {
		return SingletonHolder._instance;
	}

	private UserCommandHandler() {
		_datatable = new FastMap<Integer, IUserCommandHandler>();
	}

	public void registerUserCommandHandler(IUserCommandHandler handler) {
		int[] ids = handler.getUserCommandList();
		for (int i = 0; i < ids.length; i++) {
			if (_log.isDebugEnabled())
				_log.debug("Adding handler for user command " + ids[i], Config.SERVER_ID);
			_datatable.put(Integer.valueOf(ids[i]), handler);
		}
	}

	public IUserCommandHandler getUserCommandHandler(int userCommand) {
		if (_log.isDebugEnabled())
			_log.info("getting handler for user command: " + userCommand,Config.SERVER_ID);
		return _datatable.get(Integer.valueOf(userCommand));
	}

	/**
	 * @return
	 */
	public int size() {
		return _datatable.size();
	}

	@SuppressWarnings("synthetic-access")
	private static class SingletonHolder {
		protected static final UserCommandHandler _instance = new UserCommandHandler();
	}
}
